<?php

class Model_BidItem extends Model_Entities_BidItem
{

    public  function buildAttributes($params) {
        $auth = Zend_Auth::getInstance()->getStorage('FO');
        $userProfile = $auth->read();
        $attributes = array();
        if(isset($params['id']) && $params['id'] > 0) {
            $attributes['id'] = $params['id'];
            $attributes['modified_date'] = time();
            $attributes['modified_by'] = $userProfile['id'];
        }
        else {
            $attributes['created_by'] = $userProfile['id'];
            $curentTime = time();
            $attributes['start_date'] = date('Y-m-d H:i:s', $curentTime);
            $attributes['end_date'] = date('Y-m-d H:i:s', $curentTime + $params['frmItemEnd']*24*3600);
        }
        $attributes['title'] = $params['frmItemNew'];
        $attributes['category_id'] = $params['frmItemCat'];
        $attributes['sub_category_id'] = $params['frmItemSubCat'];
        $attributes['item_status'] = $params['frmItemChosen'];
        $attributes['reserve_price'] = $params['frmItemReserve'];
        $attributes['start_price'] = $params['frmItemStart'];
        $attributes['buynow_price'] = $params['frmItemSellnow'];
        $attributes['bid_range_amount'] = $params['frmItemBidStep'];
        $attributes['description'] = $params['frmItemDescription'];
        $attributes['published'] = 1;
        $attributes['bid_status'] = 1;
        $attributes['featured_image'] = $params['image'];
        $attributes['thumbnails'] = $params['thumbnails'];
        $attributes['galleries'] = $params['images'];
        return $attributes;
    }

    public function updatePublished($ids, $value) {
        $mapper = $this->getMapper();
        $dbTable = $mapper->getDbTable();
        $data = array('published' => intval($value));
        if(is_array($ids)) {
            $where = 'id IN ('.implode(', ', $ids).')';
        }
        else if($ids > 0){
            $where = 'id IN ('.$ids.')';
        }
        $numRows = $dbTable->update($data, $where);

        return $numRows;
    }
}